<template>
  <div class="login-main">
    <div class="login-main-from">
      <div class="login-container">
        <el-form ref="loginForm" :model="loginForm"  class="login-form" auto-complete="on" label-position="left">

          <div class="title-container">
            <h1 class="title">账号注册</h1>
          </div>

          <el-form-item prop="jobs" :rules="loginRules.jobs">
            <span style="font-size: 23px;" class="svg-container">
              <svg-icon icon-class="userlist" />
            </span>
            <el-select v-model="loginForm.jobs" placeholder="请选择您的岗位" >
              <el-option label="岗位一" value="shanghai"></el-option>
              <el-option label="岗位二" value="beijing"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item prop="username" :rules="loginRules.username" >
            <span style="font-size: 23px;" class="svg-container">
              <svg-icon icon-class="user" />
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入您的姓名"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="phone" :rules="loginRules.phone">
            <span style="font-size: 23px;" class="svg-container">
              <svg-icon icon-class="phone" />
            </span>
            <el-input
              ref="phone"
              v-model="loginForm.phone"
              placeholder="请输入您的账号"
              name="phone"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <!-- <el-form-item prop="code" :rules="loginRules.code">
            <el-col :span="2">
              <span style="font-size: 23px;" class="svg-container">
                <svg-icon icon-class="code" />
              </span>
            </el-col>
            <el-col :span="11">
              <el-input
                ref="code"
                v-model="code"
                placeholder="请输入短信验证码"
                name="code"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-col>
            <el-col :span="11" class="cou-button">
              <el-button @click="sendcode" size="medium" v-if="issend" type="primary"
                >点击获取验证码</el-button
              >
              <el-button v-if="!issend" disabled 
                >({{ count }}s)获取验证码</el-button
              >
            </el-col>
          </el-form-item> -->

          <el-form-item prop="password" :rules="loginRules.password">
            <span style="font-size: 23px;" class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入您的密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-form-item prop="repassword" :rules="loginRules.repassword">
            <span style="font-size: 23px;" class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordReType"
              ref="repassword"
              v-model="loginForm.repassword"
              :type="passwordReType"
              placeholder="请确认您的密码"
              name="repassword"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showRePwd">
              <svg-icon :icon-class="passwordReType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">注册</el-button>

        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    return {
      issend: true,
      codePadding: 10,
      count: 0,
      code: "",
      loginForm: {
        jobs:'',
        username: '',
        phone:'',
        password: '',
        repassword: ''
      },
      loginRules: {
        jobs: [{ required: true, message: '*岗位不能为空!',trigger: 'change', validator: (rule, value, callback) => {
                if (value=='') {
                  callback(new Error('*岗位不能为空!'))
                } else {
                  this.$refs['loginForm'].clearValidate(['jobs']);
                  callback()
                }
              }
        }],
        username: [{ required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*姓名不能为空!'))
                } else if(value.length<2){
                  callback(new Error('*请输入正确姓名!'))
                } else {
                  this.$refs['loginForm'].clearValidate(['username']);
                  callback()
                }
              }
        }],
        phone: [{ required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*账号不能为空!'))
                } else if(value.length!=11){
                  callback(new Error('*请输入正确的手机号!'))
                } else {
                  this.$refs['loginForm'].clearValidate(['phone']);
                  callback()
                }
              }
        }],
        code: [{ required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*验证码不能为空!'))
                } else {
                  this.$refs['loginForm'].clearValidate(['code']);
                  callback()
                }
              }
        }],
        password: [{ required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*密码不能为空!'))
                } else if (value.length < 6) {
                  callback(new Error('密码长度不能小于6位!'))
                } else {
                  this.$refs['loginForm'].clearValidate(['password']);
                  callback()
                }
              }
        }],
        repassword: [{ required: true, trigger: 'blur', validator: (rule, value, callback) => {
                if (value=="") {
                  callback(new Error('*确认密码不能为空!'))
                } else if (value === this.loginForm.password) {
                  callback(new Error('*输入密码不一致!'))
                } else {
                  this.$refs['loginForm'].clearValidate(['repassword']);
                  callback()
                }
              }
        }],
      },
      loading: false,
      passwordType: 'password',
      passwordReType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    sendcode() {
      this.codePadding = 4;
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        this.issend = false;
        //这里可以插入$axios调用后台接口
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
          } else {
            this.issend = true;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
        // this.registerForm.validateFields((err, values) => {
        //   userApi.SMScode(values.phone).then(res=>{
        //     this.code = res.data
        //   })
        // });
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    showRePwd(){
      if (this.passwordReType === 'password') {
        this.passwordReType = ''
      } else {
        this.passwordReType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.repassword.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // this.loading = true
          // this.$store.dispatch('user/login', this.loginForm).then(() => {
          //   this.$router.push({ path: this.redirect || '/' })
          //   this.loading = false
          // }).catch(() => {
          //   this.loading = false
          // })
          this.$router.push("/")
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#E5E5E5;
$light_gray:#000;
$cursor: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 90%;
    padding-left: 6px;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    font-size: 16px;
    margin-bottom: 16px;
  }
}
</style>

<style lang="scss" scoped>
// $bg:#eee;
$dark_gray:#889aa4;
$light_gray:#454545;

.login-container {
  // min-height: 100%;
  // width: 100%;
  // background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 400px;
    // max-width: 100%;
    // padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #000;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      color: $light_gray;
      margin: 0px auto 0px auto;
      text-align: center;
      font-weight: 900;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>

<style scoped>
  .login-main{
        display: flex;
        justify-content:center;
        align-items:center;
        background-image: url(../../assets/login-background.jpg);
        background-size: 100% 100%;
        background-repeat: no-repeat;
        height: 100%;
        /* position: fixed; */
        width: 100%;
  }
  .login-main-from{
      /* height: 400px; */
      width: 500px;
      border-radius: 8px;
      padding: 20px;
      background-color: #fff;
  }
  .register-active{
    display: flex;
    justify-content:center;
    align-items:center;
  }
  .cou-button{
    display: flex;
    justify-content: flex-end;
    height: 52px;
  }
</style>